
#ifndef _SMS_H_
#define _SMS_H_

#define CYCLES_PER_LINE 228

enum {
  SLOT_BIOS   = 0,
  SLOT_CARD   = 1,
  SLOT_CART   = 2,
  SLOT_EXP  = 3
};

enum {
  MAPPER_NONE     = 0,
  MAPPER_SEGA     = 1,
  MAPPER_CODIES     = 2,
  MAPPER_KOREAN     = 3
};

enum {
  DISPLAY_NTSC    = 0,
  DISPLAY_PAL     = 1
};

enum {
  FPS_NTSC    = 60,
  FPS_PAL     = 50
};

enum {
  CLOCK_NTSC      = 3579545,
  CLOCK_PAL       = 3546895,
	CLOCK_NTSC_SMS1 = 3579527
};

enum {
  CONSOLE_SMS     = 0x20,
  CONSOLE_SMS2    = 0x21,

  CONSOLE_GG      = 0x40,
  CONSOLE_GGMS    = 0x41,

  CONSOLE_MD      = 0x80,
  CONSOLE_MDPBC   = 0x81,
  CONSOLE_GEN     = 0x82,
  CONSOLE_GENPBC  = 0x83
};

#define HWTYPE_SMS  CONSOLE_SMS
#define HWTYPE_GG   CONSOLE_GG
#define HWTYPE_MD   CONSOLE_MD

#define IS_SMS    (sms.console & HWTYPE_SMS)
#define IS_GG     (sms.console & HWTYPE_GG)
#define IS_MD     (sms.console & HWTYPE_MD)

enum {
  TERRITORY_DOMESTIC  = 0,
  TERRITORY_EXPORT    = 1
};

/* SMS context */
typedef struct
{
  uint8 wram[0x2000];
  uint8 paused;
  uint8 save;
  uint8 territory;
  uint8 console;
  uint8 display;
  uint8 fm_detect;
  uint8 glasses_3d;
  uint8 hlatch;
  uint8 use_fm;
  uint8 memctrl;
  uint8 ioctrl;
  struct {
    uint8 pdr;    /* Parallel data register */
    uint8 ddr;    /* Data direction register */
    uint8 txdata;   /* Transmit data buffer */
    uint8 rxdata;   /* Receive data buffer */
    uint8 sctrl;  /* Serial mode control and status */
  } sio;
  uint8 device[2];
  uint8 gun_offset;
} sms_t;

/* BIOS ROM */
typedef struct
{
	uint8 *rom;
	uint8 enabled;
	uint8 pages;
	uint8 fcr[4];
} bios_t;

typedef struct
{
	uint8 *rom;
	uint8 pages;
	uint8 *fcr;
	uint8 mapper;
} slot_t;

/* Global data */
extern sms_t sms;
extern bios_t bios;
extern slot_t slot;

extern uint8 dummy_write[0x400];
extern uint8 dummy_read[0x400];

/* Function prototypes */
void sms_init(void);
void sms_reset(void);
void sms_shutdown(void);
void mapper_reset(void);
void sms_mapper_w(int address, int data);
int sms_irq_callback(int param);

#endif /* _SMS_H_ */
